﻿using IMES.Common.Domain.DariusModel;
using IMES.Common.Domain.KeepModel;
using IMES.Common.Domain.KeepModel.Process;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using Keep.API.Common.L.Dtos;
using MediatR;
using Microsoft.AspNetCore.Http.HttpResults;
using NPOI.SS.Formula.Functions;

namespace Keep.API.Appliction.Command.workSheetFile
{
	/// <summary>
	/// 工单列表
	/// </summary>
	public class workSheetListHandler : IRequestHandler<workSheetListCommon, ResultPage<workSheetListDto>>
	{
		private readonly IFreeSql _freeSql;
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="freeSql"></param>
		public workSheetListHandler(IFreeSql freeSql)
		{
			_freeSql = freeSql;
		}
		/// <summary>
		/// 处理
		/// </summary>
		/// <param name="request"></param>
		/// <param name="cancellationToken"></param>
		/// <returns></returns>
		/// <exception cref="NotImplementedException"></exception>
		public Task<ResultPage<workSheetListDto>> Handle(workSheetListCommon request, CancellationToken cancellationToken)
		{

			//开启一个事务 完成业务操作以及publish发布消息  
			var result = new ResultPage<workSheetListDto>() { Code = (int)CodeEnum.search, Messege = "查询失败" };
			try
			{

				var query = _freeSql.Select<workSheet, TaskTable, Product>()
			   .InnerJoin((w, t, p) => w.WorkSheetNumber == t.WorkSheetNumber)
				   .InnerJoin((w, t, p) => w.ProductNumber == p.ProductNumber)
			   .WhereIf(!string.IsNullOrEmpty(request.WorkSheetNumber),
				(w, t, p) => w.WorkSheetNumber == request.WorkSheetNumber)
					.ToList((w, t, p) => new workSheetListDto
					{
						WorkSheetId = w.WorkSheetId,
						WorkSheetNumber = w.WorkSheetNumber,
						ProductNumber = w.ProductNumber,
						ProductName = p.ProductName,
						ProductStandard = p.ProductSpecification,
						ProductUnit = p.StockUnit,
						ProductRelatedDocuments = w.ProductRelatedDocuments,
						Numberofpackages = w.Numberofpackages,
						State = w.State,
						Remark = w.Remark,
						TaskIds = w.TaskIds,
						TaskId = t.TaskId,
						ProcessName = t.ProcessName,
						ReportingAuthority = t.ReportingAuthority,
						Matching = t.Matching,
						AdverseList = t.AdverseList,
						ScheduledStartTime = w.ScheduledStartTime,
						ScheduledEndTime = w.ScheduledEndTime,
						RealQuantity = t.RealQuantity,
						GoodScore = t.GoodScore,
						DefectiveQuantity = t.DefectiveQuantity,
						ActualTime = t.ActualTime,
						ActualStartTime = w.StartTime,
						ActualEndTime = w.EndTime,
						TaskState = t.State,
						CreateId = w.CreateId,
						CreateName = w.CreateName,
						CreateDate = w.CreateDate,
						UpdateName = w.UpdateName,
						UpdateDate = w.UpdateDate,
					});

				if (query == null)
				{
					//弹出异常
					throw new Exception("查询异常");
				}
				//分页 

				result.totalCount = query.Count();
				result.totalPage = (int)Math.Ceiling(result.totalCount / (double)request.pageSize);
				result.Data = query.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
				result.Code = (int)CodeEnum.success;
				result.Messege = "查询成功";
				return Task.FromResult(result);
			}
			catch (Exception)
			{
				result.Code = (int)CodeEnum.error;
				result.Messege = "查询异常";
				throw;
			}



		}
	}
}
